//
//  caiwurizhiViewController.m
//  jypt_ios
//
//  Created by 千翼网络科技 on 2017/9/23.
//  Copyright © 2017年 xp. All rights reserved.
//

#import "caiwurizhiViewController.h"
#import "caiwurizhiTableViewCell.h"
#import "MJRefresh.h"
#import "caiWuRiZhiDetailViewController.h"
@interface caiwurizhiViewController ()<UITableViewDelegate , UITableViewDataSource>{
    
    int page;
    int mjMark;
    
}

@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,strong) NSMutableDictionary *dataDic;
@property(nonatomic,strong) UITableView *myTableView;

@end

@implementation caiwurizhiViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    //下拉刷新
    _myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        mjMark = 1;
        page = 1;
        [self requestData];
        [_myTableView.mj_header endRefreshing];
        
    }];
    
    //上拉加载
    _myTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        mjMark = 2;
        page ++;
        [self requestData];
        [_myTableView.mj_footer endRefreshing];
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"财务记录";
    self.view.backgroundColor = getColor(@"ffffff");
    [self initView];
    [self requestData];
}

#pragma mark ------------------ requestData -------------------

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = NO;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue:[PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue: [NSString stringWithFormat:@"%d", page] forKey:@"page"];
    
    [[MNetworkHandler sharedInstance]conURL:adminFinanceGetFinance
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       if (mjMark == 1) {
                                           
                                           _dataArray = returnData[@"data"];
                                           
                                       }else{
                                           
                                           [_dataArray addObjectsFromArray:returnData[@"data"]];
                                           
                                       }
                                       NSLog(@"\n\n\n === %@",_dataArray);
                                       
                                       [_myTableView reloadData];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                       
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
//                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

#pragma mark ------------------ init -------------------

- (void)initView{
    
    _dataArray = [[NSMutableArray alloc]init];
    mjMark = 1;
    page = 1;
    UIView *viewOf = [[UIView alloc] init];
    viewOf.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, 1);
    [self.view addSubview:viewOf];
    viewOf.backgroundColor = [UIColor colorWithRed:204 / 255.0f green:204 / 255.0f  blue:204 / 255.0f  alpha:1];
    
    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 1, scrWidth(), scrHeight() - 64)];
    _myTableView.backgroundColor = getColor(@"ffffff");
    _myTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTableView];
}

#pragma mark ------------------ click -------------------

- (void)searchBtnClick{
    
    NSLog(@"bar btn");
    
}

#pragma mark -------------------- tableView ----------------------

#pragma mark --- UITableViewDelegate ---

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return screenScale(70);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    caiWuRiZhiDetailViewController *vc = [[caiWuRiZhiDetailViewController alloc] init];
    
    NSMutableDictionary *dic = self.dataArray[indexPath.row];
    
    vc.datailID = [dic objectForKey:@"id"];
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    
}

#pragma mark --- UITableViewDataSource ---

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView registerClass:[caiwurizhiTableViewCell class] forCellReuseIdentifier:@"identifier"];
    caiwurizhiTableViewCell *cell = (caiwurizhiTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (_dataArray) {
        
        cell.nameLabel.text = _dataArray[indexPath.row][@"finance_type"];
        cell.contentLabel.text = _dataArray[indexPath.row][@"content"];
        cell.dateLabel.text = _dataArray[indexPath.row][@"add_time"];
        
        if ([_dataArray[indexPath.row][@"type"] isEqualToString:@"1"]) {
            
            cell.numLabel.text = [@"+" stringByAppendingString: _dataArray[indexPath.row][@"money"]];
            
        }else{
            
            cell.numLabel.text = [@"-" stringByAppendingString: _dataArray[indexPath.row][@"money"]];
            
        }
        
    }
    
    return cell;
}

@end
